
import { monthRange, parseTime, createDownload } from '@/utils'
import { queryDownloadData } from '@/api/display/recommendReport'
export default {
  props: {
    categoryList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      isExporting: false,
      months: [],
      formData: { month: '', categoryName: '' }
    }
  },
  computed: {
  },
  created() {
    this.formData.categoryName = this.categoryList.length ? this.categoryList[0].dictName || '' : ''
    const date = new Date()
    date.setMonth(date.getMonth() - 9)
    this.months = [parseTime(date, '{y}-{m}'), parseTime(new Date(), '{y}-{m}')]
    this.formData.month = monthRange(this.months[0], this.months[1]).join(',')
  },
  watch: {

    categoryList(v, o) {
      this.formData.categoryName = v && v.length > 0 ? v[0].dictName || '' : ''
    },
    formData: {
      handler(v) {
        this.initTableData()
      },
      deep: true,
      immediate: true
    }
  },
  methods: {

    handleExport() {
      this.isExporting = true
      queryDownloadData(this.formData, this.exportUrl).then(res => {
        if (res) {
          createDownload(res, this.exportName)
        }
        this.isExporting = false
      }).catch(() => {
        this.isExporting = false
      })
    },
    changeMonth(data) {
      if (data) {
        this.formData.month = monthRange(data[0], data[1]).join(',')
      }
    }
  }
}
